var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    //是否采用衔接滑动  
    circular: true,
    //是否显示画板指示点  
    indicatorDots: true,
    //选中点的颜色  
    indicatorcolor: "#000",
    //是否竖直  
    vertical: false,
    //是否自动切换  
    autoplay: true,
    //自动切换的间隔
    interval: 2500,
    //滑动动画时长毫秒  
    duration: 100,
    //所有图片的高度  （必须）
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  （必须）
    current: 0,
    currentTab: 0,
    info: [],
    goodsId: 0,
    goodsDetailsHtml: '',
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    goodsid: "", //商品ID
    goodsnum: 1, //商品数量
    etype: 1, //商品类型；1=术前术后产品，2=服务类产品，默认1
    take: !0,
    options: "",
    entryType:'',
    latitude:'',
    longitude:''
  },

  onLoad: function(options) {
    app.showLoading();
    const that= this;
    this.setData({
      entryType: wx.getStorageSync('entryType') //获取进入方式隐藏购买
    })
    this.loadData(options);
    if (options.image) {
      this.setData({
        image: options.image,
        roleType: wx.getStorageSync('roleType'),
        etype: options.etype,
      })
    }
  
    this.setData({
      options: options,
    })
  },

  // 加载数据
  loadData: function (options) {
    var that = this;
    this.setData({
      'etype': options.etype,
      'url': app.globalData.imgUrl,
      'goodsid': options.id
    });
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        var lngandlat = res.longitude + ',' + res.latitude;
        if (options.etype == 2) {
          // this.setData({
          //   'etype': options.etype,
          //   'url': app.globalData.imgUrl,
          //   goodsid: options.id,
          //   etype: options.etype,
          // })


          // if (options.etype == 1) {
          //   url = 'shopp/yimeiGoodsApi/getDetail/';
          // } else {

          // }
          app.request({
            url: 'shopp/goodsApi/goodsDetail/' + options.id,
            method: 'GET',
            success(res) {
              wx.hideLoading();
              if (res.status_code == 200) {
                if (res.data.goodsDetail.ifCollection == 0) {
                  that.setData({
                    take: !0
                  })
                } else {
                  that.setData({
                    take: !1
                  })
                }
                that.setData({
                  info: res.data,
                  goodsId: options.id,
                })
              }
            }
          })
        } else {
          that.setData({
            'etype': options.etype,
            'url': app.globalData.imgUrl,
            goodsid: options.id
          });

          // var longitude = wx.getStorageSync('longitude');
          // var latitude = wx.getStorageSync('latitude');
          // var lngandlat = longitude + ',' + latitude;
          // var url = '';

          // if (options.etype == 1) {

          // } else {
          //   url = 'shopp/goodsApi/goodsDetail/';
          // }
          app.request({
            url: 'shopp/yimeiGoodsApi/getDetail/' + options.id + '?lngandlat=' + lngandlat,
            method: 'GET',
            success(res) {
              wx.hideLoading();
              if (res.status_code == 200) {
                if (res.data.goodsDetail.ifCollection == 0) {
                  that.setData({
                    take: !0
                  })
                } else {
                  that.setData({
                    take: !1
                  })
                }
                that.setData({
                  doctorId: res.data.doctor.userId,
                  info: res.data,
                  goodsId: options.id,
                })
              }
            }
          })
        }

        // app.request({
        //   url: 'shopp/yimeiGoodsApi/getDetail/' + options.id + '?lngandlat=' + lngandlat,
        //   method: 'GET',
        //   success(res) {
        //     if (res.status_code == 200) {
        //       if (res.data.goodsDetail.ifCollection == 0) {
        //         that.setData({
        //           take: !0
        //         })
        //       } else {
        //         that.setData({
        //           take: !1
        //         })
        //       }
        //       that.setData({
        //         doctorId: res.data.doctor.userId,
        //         info: res.data,
        //         goodsId: options.id,
        //       })
        //     }
        //   }
        // })

      }
    })

  },

  consult: function () {
    var that = this;
    var isLogin = wx.getStorageSync('isLogin');
    const {
      info
    } = this.data;
    if (isLogin != 2) {
      wx.navigateTo({
        url: `../user-my-kf/user-my-kf?doctorId=${info.doctor.userId}`,
      });
    } else {
      wx.reLaunch({
        url: '/pages/doctor-login/doctor-login',
      });
    }
  },
  // 加入购物车
  addCart: function (e) {
    const that = this;
    const param = {
      goodsid: this.data.goodsid,
      goodsnum: this.data.goodsnum,
      type: 1,
    };
    app.request({
      url: 'shopp/shoppcartApi/cartAddGoods',
      data: param,
      success: function (res) {
        wx.reLaunch({
          url: '/pages/user-my-cart/user-my-cart?id=' + that.data.goodsid
        })
      },
      fail: function (res) { }
    });
  },

  imageLoad: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;

    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里
    imgheights.push(imgheight)
    this.setData({
      imgheights: imgheights,
    })
  },

  bindchange: function (e) {
    this.setData({
      current: e.detail.current
    })
  },

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    if(e.detail.current==1){
      this.loadDetail();
    }
  },
  //点击切换
  clickTab: function(e) {
    const current = e.target.dataset.current;
    if (0 == current) {
      this.setData({
        currentTab: current
      })
    }
    if (1 == current) {
      this.setData({
        currentTab: current
      })
      this.loadDetail();
      // this.loadData()
    }

  },

  loadDetail: function(e) {
    var goodsId = this.data.goodsId;
    var that = this;
    var etype = this.data.etype;
    var url = '';
    if (etype == 1) {
      url = 'shopp/yimeiGoodsApi/getGoodsDetailsHtml/' + goodsId;
    } else {
      url = 'shopp/goodsApi/getGoodsDetailsHtml/' + goodsId;
    }
   
    app.request({
      url: url,
      method: "GET",
      success(res) {
        var detail = res.data.photo;
        that.setData({
          goodsDetailsHtml: res.data.photo,
        })
        WxParse.wxParse('detail', 'html', detail, that, 5);
      }
    })
  },
  //立刻购买
  buy() {
    var that = this;
    var isLogin = wx.getStorageSync('isLogin');
    console.log(that.data.etype)
    if (isLogin == 2 || !isLogin) {
      wx.showToast({
        title: '请登录!',
        icon: 'loading',
      })
      // 跳转到登入界面
      wx.reLaunch({
        url: '/pages/doctor-login/doctor-login'
      })
    } else {
      if (that.data.etype == 1) {
        wx.navigateTo({
          url: "../user-shop-goods-order-medical/user-shop-goods-order-medical?id=" + this.data.goodsid + "&type=" + that.data.etype,
        })
      } else if (that.data.etype == 2) {
        let cartgoods = [];
        let item = {};
        item.goodsid = this.data.goodsid;
        item.num = 1;
        cartgoods.push(item)
        const carts = JSON.stringify(cartgoods);
        wx.navigateTo({
          url: "../user-shop-goods-order-1/user-shop-goods-order-1?carts=" + carts,
        })
      }
    }
  },

  //点击收藏
  collection: function() {
    var that = this;
    var cetype = that.data.etype;
    if (cetype == 1) {
      app.request({
        url: 'doctor/member/collectGoods', //仅为示例，并非真实的接口地址
        data: {
          userId: wx.getStorageSync('userid'),
          goodsId: that.data.goodsid,
          goodsType: 2,
        },
        success(res) {
          that.setData({
            take: !that.data.take
          })
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 1000
          })
        }
      })
    } else {
      app.request({
        url: 'doctor/member/collectGoods', //仅为示例，并非真实的接口地址
        data: {
          userId: wx.getStorageSync('userid'),
          goodsId: that.data.goodsid,
          goodsType: 1,
        },
        success(res) {
          that.setData({
            take: !that.data.take
          })
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 1000
          })
        }
      })
    }

  },
  // 关注按钮
  bindFollow: function (e) {
    var userid = e.currentTarget.dataset.userid;
    var isfollow = e.currentTarget.dataset.isfollow;
    var that = this;
    if (isfollow == 1) {
      app.request({
        url: 'doctor/member/attention',
        data: {
          userId: userid,
        },
        success(res) {
          wx.showToast({
            title: '取消关注',
            icon: 'success',
            duration: 1000
          })
          that.onLoad(that.data.options);
        }
      })
    } else {
      app.request({
        url: 'doctor/member/attention',
        data: {
          userId: userid,
        },
        success(res) {
          wx.showToast({
            title: '关注成功',
            icon: 'success',
            duration: 1000
          })
          that.onLoad(that.data.options);
        }
      })
    }
  },

  // 分享
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  }

})